package com.ys.nms.module.business.controller.admin.links;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.links.vo.*;
import com.ys.nms.module.business.dal.dataobject.links.LinksDO;
import com.ys.nms.module.business.service.links.LinksService;

@Tag(name = "管理后台 - 路线-自动")
@RestController
@RequestMapping("/business/links")
@Validated
public class LinksController {

    @Resource
    private LinksService linksService;

    @PostMapping("/create")
    @Operation(summary = "创建路线-自动")
    public CommonResult<Integer> createLinks(@Valid @RequestBody LinksSaveReqVO createReqVO) {
        return success(linksService.createLinks(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新路线-自动")
    public CommonResult<Boolean> updateLinks(@Valid @RequestBody LinksSaveReqVO updateReqVO) {
        linksService.updateLinks(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除路线-自动")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteLinks(@RequestParam("id") Integer id) {
        linksService.deleteLinks(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除路线-自动")
    public CommonResult<Boolean> deleteLinksList(@RequestParam("ids") List<Integer> ids) {
        linksService.deleteLinksListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得路线-自动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<LinksRespVO> getLinks(@RequestParam("id") Integer id) {
        LinksDO links = linksService.getLinks(id);
        return success(BeanUtils.toBean(links, LinksRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得路线-自动分页")
    public CommonResult<PageResult<LinksRespVO>> getLinksPage(@Valid LinksPageReqVO pageReqVO) {
        PageResult<LinksDO> pageResult = linksService.getLinksPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LinksRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出路线-自动 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLinksExcel(@Valid LinksPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LinksDO> list = linksService.getLinksPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "路线-自动.xls", "数据", LinksRespVO.class,
                        BeanUtils.toBean(list, LinksRespVO.class));
    }

}